package com.slg.android.manyanime;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import com.slg.android.manyanime.bean.User;
import com.slg.android.manyanime.callBackInterface.AnimeCalltoMain;
import com.slg.android.manyanime.fragment.AnimeFragment;
import com.slg.android.manyanime.fragment.FoundFragment;
import com.slg.android.manyanime.fragment.MessageFragment;
import com.slg.android.manyanime.fragment.UserFragment;
import com.slg.android.manyanime.utils.DataStore;


public class MainActivity extends FragmentActivity implements View.OnClickListener, SlidingMenu.OnOpenListener, AnimeCalltoMain {
    /**
     * 当前选中的菜单图片控件id
     */
    private int currentSelectedImage = R.id.main_menu_anime_imageView;
    /**
     * 当前选中的菜单文字控件id
     */
    private int currentSelectedText = R.id.main_menu_anime_textView;
    @ViewInject(R.id.main_menu_anime_textView)
    private TextView animationMenuText;
    @ViewInject(R.id.main_menu_found_textView)
    private TextView foundMenuText;
    @ViewInject(R.id.main_menu_message_textView)
    private TextView messageMenuText;
    @ViewInject(R.id.main_menu_my_textView)
    private TextView myMenuText;
    private FragmentManager fragmentManager;
    private SlidingMenu slidingMenu;
    private ImageView userHead;
    private TextView userName;
    @ViewInject(R.id.left_menu_cartoon)
    private LinearLayout cartoonlin;
    private View cartoon;
    private View coser;
    private View zhuifan;
    private View exhibition;
    private User user;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        ViewUtils.inject(this);
        //初始化侧边栏控件
        initSlidingMenu();
        Intent intent = getIntent();
        //用户登陆后，加载侧边栏用户信息
        String login = intent.getStringExtra("login");
        if (!TextUtils.isEmpty(login) && "login".equals(login)) {
            user = DataStore.getDataStore().getUser();
            BitmapUtils bitmapUtils = DataStore.getBitmapUtils();
            bitmapUtils.display(userHead, user.getHeadPic());
            userName.setText(user.getNickname());
        }
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        AnimeFragment fragment = new AnimeFragment();
        fragment.setAnimeCalltoMain(this);
        transaction.add(R.id.main_fragment_container, fragment);
        transaction.commit();
    }

    private void initSlidingMenu() {
        slidingMenu = new SlidingMenu(this);
        //标题不保留
        slidingMenu.setMenu(R.layout.main_slidingmenu);
        slidingMenu.setBehindOffset(300);
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
        slidingMenu.setTouchmodeMarginThreshold(150);
        slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
        slidingMenu.setOnOpenListener(this);
        View parent = slidingMenu.getMenu();
        if (parent != null) {
            cartoon = parent.findViewById(R.id.left_menu_cartoon);
            if (cartoon != null) {
                cartoon.setOnClickListener(this);
            }
            coser = parent.findViewById(R.id.left_menu_coser);
            if (coser != null) {
                coser.setOnClickListener(this);
            }
            zhuifan = parent.findViewById(R.id.left_menu_zhuifan);
            if (zhuifan != null) {
                zhuifan.setOnClickListener(this);
            }
            exhibition = parent.findViewById(R.id.left_menu_exhibition);
            if (exhibition != null) {
                exhibition.setOnClickListener(this);
            }
            userHead = (ImageView) parent.findViewById(R.id.left_menu_head);
            if (userHead != null) {
                userHead.setOnClickListener(this);
            }
            userName = (TextView) parent.findViewById(R.id.left_menu_name);
        }
    }

    @OnClick({R.id.main_menu_anime_imageView, R.id.main_menu_found_imageView
            , R.id.main_menu_message_imageView, R.id.main_menu_my_imageView

    })
    public void onMenuSelected(View view) {
        ImageView selectedImage = null;
        ImageView currentImage = null;
        int id = view.getId();
        if (id != currentSelectedImage) {
            selectedImage = (ImageView) findViewById(id);
            currentImage = (ImageView) findViewById(currentSelectedImage);
            setCurrentUnselected(currentImage);
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            switch (id) {
                case R.id.main_menu_anime_imageView:
                    selectedImage.setBackgroundResource(R.drawable.mainbuttonselected_animation);
                    animationMenuText.setTextColor(getResources().getColor(R.color.main_menu_text_selected));
                    currentSelectedImage = id;
                    currentSelectedText = R.id.main_menu_anime_textView;
                    transaction.replace(R.id.main_fragment_container, new AnimeFragment());
                    break;
                case R.id.main_menu_found_imageView:
                    selectedImage.setBackgroundResource(R.drawable.mainbuttonselected_found);
                    foundMenuText.setTextColor(getResources().getColor(R.color.main_menu_text_selected));
                    currentSelectedImage = id;
                    currentSelectedText = R.id.main_menu_found_textView;
                    transaction.replace(R.id.main_fragment_container, new FoundFragment());
                    break;
                case R.id.main_menu_message_imageView:
                    if (user != null) {
                        selectedImage.setBackgroundResource(R.drawable.mainbuttonselected_message);
                        messageMenuText.setTextColor(getResources().getColor(R.color.main_menu_text_selected));
                        currentSelectedImage = id;
                        currentSelectedText = R.id.main_menu_message_textView;
                        transaction.replace(R.id.main_fragment_container, new MessageFragment());
                    } else {
                        Toast.makeText(this, "尚未登录", Toast.LENGTH_SHORT).show();
                        //TODO  添加未登录提示对话框
                    }
                    break;
                case R.id.main_menu_my_imageView:
                    selectedImage.setBackgroundResource(R.drawable.mainbuttonselected_my);
                    myMenuText.setTextColor(getResources().getColor(R.color.main_menu_text_selected));
                    currentSelectedImage = id;
                    currentSelectedText = R.id.main_menu_my_textView;
                    transaction.replace(R.id.main_fragment_container, new UserFragment());
                    break;
            }
            transaction.commit();
        }


    }

    private void setCurrentUnselected(ImageView image) {
        TextView currentTextView = (TextView) findViewById(currentSelectedText);
        currentTextView.setTextColor(getResources().getColor(R.color.main_menu_text));
        switch (currentSelectedImage) {
            case R.id.main_menu_anime_imageView:
                image.setBackgroundResource(R.drawable.mainbutton_animation);
                break;
            case R.id.main_menu_found_imageView:
                image.setBackgroundResource(R.drawable.mainbutton_found);
                break;
            case R.id.main_menu_message_imageView:
                image.setBackgroundResource(R.drawable.mainbutton_message);
                break;
            case R.id.main_menu_my_imageView:
                image.setBackgroundResource(R.drawable.mainbutton_my);
                break;
        }
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        Intent intent = null;
        switch (id) {
            case R.id.left_menu_cartoon:
                intent = new Intent(this, CartoonActivity.class);
                startActivity(intent);
                break;
            case R.id.left_menu_head:
                if (user == null) {//用户尚未登录
                    intent = new Intent(this, LoginActivity.class);
                    startActivity(intent);
                } else {
                    //TODO 跳转用户信息页面
                }
                break;
            case R.id.left_menu_coser:
                intent = new Intent(this, CoserActivity.class);
                startActivity(intent);
                break;
            case R.id.left_menu_zhuifan:
                intent = new Intent(this, ZhuifanActivity.class);
                startActivity(intent);
                break;

        }
    }


    @Override
    public void onOpen() {
        Animation first = AnimationUtils.loadAnimation(this, R.anim.left_menu_anim_1);
        Animation second = AnimationUtils.loadAnimation(this, R.anim.left_menu_anim_2);
        Animation third = AnimationUtils.loadAnimation(this, R.anim.left_menu_amin_3);
        cartoon.startAnimation(first);
        coser.startAnimation(second);
        exhibition.startAnimation(third);
    }

    @Override
    public void dosomething() {
        slidingMenu.toggle();
    }
}
